package com.fjwt.gz.sync.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.constants.ApiCodeEnum;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.AwardCouponNotifyEntity;
import com.fjwt.gz.db.entity.FansAwardEntity;
import com.fjwt.gz.db.entity.PayOrderEntity;
import com.fjwt.gz.service.impl.AwardCouponNotifyService;
import com.fjwt.gz.service.impl.FansAwardService;
import com.fjwt.gz.service.impl.PayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class PayOrderSyncService {
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private FansAwardService fansAwardService;
    @Autowired
    private AwardCouponNotifyService awardCouponNotifyService;


    @Transactional
    public ApiRes syncPayOrder(String decryptSign, String ifCode, String urlOrderId) throws Exception {
        log.info("decryptSign:{},ifCode:{},urlOrderId:{}", decryptSign, ifCode, urlOrderId);
        JSONObject decryptSignJson = JSONObject.parseObject(decryptSign);
        String payOrderStr = decryptSignJson.getString("payOrder");
        JSONObject payOrderJson = JSONObject.parseObject(payOrderStr);
        PayOrderEntity payOrderEntity = JSONObject.parseObject(payOrderStr, PayOrderEntity.class);
        log.info("payOrderEntity:{}", JSONUtil.toJsonStr(payOrderEntity));
        log.info("activityId:{}", payOrderJson.getString("activityId"));
        log.info("promotionId:{}", payOrderJson.getString("promotionId"));
        payOrderEntity.setStockId(payOrderJson.getString("activityId"));
        payOrderEntity.setCouponThirdId(payOrderJson.getString("promotionId"));

        String payOrderId = payOrderEntity.getPayOrderId();
        PayOrderEntity payOrderDb = payOrderService.getById(payOrderId);
        log.info("payOrderDb:{}", JSONUtil.toJsonStr(payOrderDb));
        if (payOrderDb != null) {
            return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, "此订单已经推送过");
        }
        //核销卡券
        LambdaQueryWrapper<FansAwardEntity> gw = FansAwardEntity.gw();
        gw.eq(FansAwardEntity::getCouponThirdId, payOrderJson.getString("promotionId"));
        FansAwardEntity fansAwardEntity = fansAwardService.getBaseMapper().selectOne(gw);
        log.info("fansAwardEntity:{}", JSONUtil.toJsonStr(fansAwardEntity));
        if (ObjectUtil.isEmpty(fansAwardEntity)) {
            log.info("此卡券不是在营销平台创建");
            return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, "此卡券不是在营销平台创建");
        }
        AwardCouponNotifyEntity awardCouponNotifyEntity = new AwardCouponNotifyEntity();
        String code = awardCouponNotifyService.getBaseMapper().selectIfCode(payOrderEntity.getCouponThirdId());
        if (StringUtils.isEmpty(code)) {
            awardCouponNotifyEntity.setIfcode(ifCode);
        } else {
            awardCouponNotifyEntity.setIfcode(code);
        }
        awardCouponNotifyEntity.setCouponThirdId(payOrderEntity.getCouponThirdId());
        awardCouponNotifyEntity.setState(1);
        if (payOrderEntity.getState() == 2) {
            awardCouponNotifyEntity.setNotifyState(0);
        }
        awardCouponNotifyEntity.setNotifyJson(JSONUtil.toJsonStr(payOrderEntity));
        Date date = new Date();
        awardCouponNotifyEntity.setCreatedAt(date);
        awardCouponNotifyEntity.setUpdatedAt(date);
        awardCouponNotifyEntity.setSourceType(0);
        log.info("添加回调信息为：{}", JSONUtil.toJsonStr(awardCouponNotifyEntity));
        awardCouponNotifyService.save(awardCouponNotifyEntity);


        if (fansAwardEntity.getState() == 1) {
            throw new BizException("此订单已经推送过");
        }

        if (fansAwardEntity != null) {
            payOrderEntity.setIsUse(0);
            payOrderService.getBaseMapper().insert(payOrderEntity);
            ApiRes a = ApiRes.ok("成功");
            log.info("-----a:{}", JSONUtil.toJsonStr(a));
            return ApiRes.ok("成功");
        }
        return null;
    }
}
